package com.spring.boot.controller;

import com.alibaba.druid.support.json.JSONUtils;
import com.google.gson.Gson;
import com.spring.boot.default1.mapper.DefaultDemoMapper;
import com.spring.boot.default1.mapper.DefaultDemoMapper2;
import com.spring.boot.model.Stu;
import org.springframework.beans.BeansException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.text.SimpleDateFormat;
import java.util.*;

@Controller
@RequestMapping("/test")
public class TestController  implements ApplicationContextAware {

    @Autowired
    private DefaultDemoMapper demoMapper;
    @Autowired
    private DefaultDemoMapper2 demoMapper2;

    private ApplicationContext applicationContext;



    @RequestMapping("query")
    @Transactional
    public @ResponseBody
    Stu queryShop(@RequestParam("shopId") Integer shopId) {
        Stu map1 = demoMapper.queryShop(1L);
        Stu map11 = demoMapper.queryShop(2L);
        System.out.println(map1==map11);
        return map1;
    }
    @RequestMapping("query2")
    public @ResponseBody
    Stu queryShop2(@RequestParam("shopId") Integer shopId) {
         demoMapper.setName(new Stu(4L,"小一"));
        Stu map11 = demoMapper.queryShop(4L);
        return map11;
    }

    @RequestMapping("session")
    public ModelAndView session(HttpServletRequest request) {

        Object attrValue = request.getSession().getAttribute("testAttrName");
        if (attrValue == null) {
            request.getSession().setAttribute("testAttrName", new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()));
        }
        Map<String,Object> contextMap = new HashMap<>();

        Enumeration<String> attributeNames = request.getSession().getAttributeNames();
        Map<String,Object> sessionMap = new HashMap<>();
        while (attributeNames.hasMoreElements()){
            String nextElement = attributeNames.nextElement();
            sessionMap.put(nextElement,request.getSession().getAttribute(nextElement));

        }
        contextMap.put("session",sessionMap);
        contextMap.put("cookie",request.getCookies());
        contextMap.put("host",request.getRemoteHost());
        String data = new Gson().toJson(contextMap);
        return new ModelAndView("jsonview", "data",
                data);
    }

    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        this.applicationContext = applicationContext;
    }
}
